<?php

class Ext_Publish_Attachment_Add_Adapter_Music implements Ext_Publish_Attachment_Add_Interface
{
    protected $_articleId;
    protected $_url;
    
    protected $_messager;
    const MUSIC_EXISTS              = 'musicExists';
    protected $_messageTemplates = array(
        self::MUSIC_EXISTS              => "Music already exists.",
    );
    
    public function __construct($articleId, $url)
    {
        $this->_articleId = $articleId;
        $this->_url       = $url;
        $this->_messager  = new Ext_Messager($this->_messageTemplates);
    }
    
    public function add()
    {
        $result = array(
            'code'      => Ext_Publish_Attachment_Add_Result::FAILURE,
            'datas'     => array(),
            'messages'  => array(),
        );
        
        $fetcher = new Ext_Music_Fetch($this->_url);
        $fetchResulter = $fetcher->fetch();
        if(!$fetchResulter->isValid())
        {
            $result['messages'] = $fetchResulter->getMessages();
            return new Ext_Publish_Attachment_Add_Result($result['code'], $result['datas'], $result['messages']);
        }
        $datas = $fetchResulter->getDatas();
        $name   = $datas['name'];
        $singer = $datas['singer'];
        
        $musicSaver = new Ext_Music_Save($datas);
        $musicSaveResulter = $musicSaver->save();
        if(!$musicSaveResulter->isValid())
        {
            $result['messages'] = $musicSaveResulter->getMessages();
            return new Ext_Publish_Attachment_Add_Result($result['code'], $result['datas'], $result['messages']);
        }
        $musicId = $musicSaveResulter->getMusicId();
        
        $attachmentMusicRow = App_Model_DbTable_Article_Attachment_Music::getInstance()->getRowByArticleId($this->_articleId);
        if($attachmentMusicRow){
            $ids = $attachmentMusicRow['ids'];

            $musicIdArray = array();
            $idsArray = explode(';', $ids);
            for($i=0;$i<count($idsArray);$i++)
            {
                $attachmentMusicId = $idsArray[$i];
                if(!$attachmentMusicId){
                    continue;
                }

                $attachmentMusic = App_Model_DbTable_Attachment_Music::getInstance()->getRowById($attachmentMusicId);
                if(!$attachmentMusic){
                    continue;
                }
                $musicIdArray[] = $attachmentMusic['musicId'];
            }
                    
            if(in_array($musicId, $musicIdArray)){
                 $result['messages'][] = $this->_messager->addMessage(self::MUSIC_EXISTS);
                 return new Ext_Publish_Attachment_Add_Result($result['code'], $result['datas'], $result['messages']);
            }
        }
        
        $row = array(
            'articleId' => $this->_articleId,
            'musicId'   => $musicId,
            'name'      => $name,
            'singer'    => $singer,
        );
        $attachmentMusicId = App_Model_DbTable_Attachment_Music::getInstance()->addNew($row);
        
        $musicer = new Ext_Article_Attachment_Music($this->_articleId);
        $musicer->add($attachmentMusicId);
        
        $articleRow = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$articleRow){
            throw new Ext_Publish_Attachment_Add_Exception("Article record not found.");
        }
        
        $attachmentsArray = unserialize($articleRow['attachments']);
        if(!is_array($attachmentsArray)){
            $attachmentsArray = array();
        }
        $attachment = array("type"=>'music', "id"=>$attachmentMusicId);
        array_unshift($attachmentsArray, $attachment);
        if($attachmentsArray){
            $row = array(
                'attachments'  => serialize($attachmentsArray),
            );
            App_Model_DbTable_Article::getInstance()->addOld($row, $this->_articleId);
        }
        
        $result['code']                       = Ext_Publish_Attachment_Add_Result::SUCCESS;
        $result['datas']['attachmentMusicId'] = $attachmentMusicId;
        return new Ext_Publish_Attachment_Add_Result($result['code'], $result['datas'], $result['messages']);
    }
}
